package com.wd.visitor.common.controller;

import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wd.common.core.domain.HttpResponse;
import com.wd.visitor.common.domain.dto.EventQuery;
import com.wd.visitor.common.domain.dto.response.AlarmData;
import com.wd.visitor.common.domain.dto.response.EventData;
import com.wd.visitor.common.domain.dto.response.ImageListResponse;
import com.wd.visitor.common.domain.dto.response.Pagination;
import com.wd.visitor.common.domain.vo.PageData;
import com.wd.visitor.common.domain.vo.Record;
import com.wd.visitor.common.service.AlarmService;
import com.wd.visitor.common.service.ImageService;
import com.wd.visitor.common.utils.TimeUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Encoder;

import javax.annotation.Resource;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;

/**
 * @className: EventController
 * @description: TODO 类描述
 * @author: liumingkun
 * @date: 2024/11/3
 **/

@RestController
public class EventController {

    @Resource
    ImageService imageService;

    @Resource
    AlarmService alarmService;

    @RequestMapping("/event/list")
    public HttpResponse getEventData(EventQuery eventQuery) {

        ImageListResponse event = imageService.getImageList(eventQuery);
        Pagination pagination = event.getPagination();
        List<EventData> data = event.getData();

        PageData pageData = new PageData();
        pageData.setAuthorization(1);
        pageData.setResult_num(pagination.getPageSize());
        pageData.setTotal_num(pagination.getTotal());

        List<Record> recordList = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            EventData eventData = data.get(i);
            Record record = new Record();
            record.setCamera_name("通道" + eventData.getChannel_id());
            record.setTrigger(TimeUtils.parseStr(eventData.getTimestamp()));
            record.setChannel(eventData.getChannel_id());
            record.setSnap_path("/image/edge?file=" + eventData.getTarget_image_path());
            record.setSnap_id(String.valueOf(eventData.getTarget_id()));
            record.setEvents_type("901");
            record.setFirst_alarm_type("901");
            recordList.add(record);
        }
        pageData.setRecord(recordList);

        HttpResponse httpResponse = HttpResponse.successData(pageData);
        httpResponse.setCode(0);
        return httpResponse;

    }

    @RequestMapping("/event/list1")
    public String getList() {
        return "{\"code\":0,\"data\":{\"authorization\":1,\"record\":[{\"body_attr\":{},\"camera_name\":\"通道1\",\"channel\":1,\"crowd_num\":3,\"crowd_threshold\":3,\"detail_type\":1,\"events_type\":\"398\",\"first_alarm_type\":\"398\",\"helmet_type\":\"\",\"leave_num_threshold\":0,\"leave_time_threshold\":0,\"over_boundary_direction\":0,\"over_boundary_num1\":0,\"over_boundary_num2\":0,\"pos_bottom\":1000,\"pos_left\":216,\"pos_right\":1848,\"pos_top\":160,\"position\":\"\",\"quality\":83,\"snap_id\":\"v214_1730626668_7503_0\",\"snap_path\":\"record/body/channel1/2024/11/03/v214_1730626668_7503_0.jpg\",\"trigger\":\"2024-11-03 17:37:48\",\"with_oiltanker\":false,\"work_cap_color\":0,\"work_cloth_color\":\"\",\"work_cloth_type\":\"\"}],\"result_num\":1,\"total_num\":2},\"msg\":\"\"}";
    }

    @RequestMapping("/alarm/list1")
    public HttpResponse alarmList() {
        EventQuery eventQuery = new EventQuery();
        eventQuery.setQry_len(5);
        eventQuery.setStart_no(1);
        eventQuery.setStart_time(TimeUtils.getStartTime());
        eventQuery.setStop_time(TimeUtils.getEndTime());


        ImageListResponse event = imageService.getImageList(eventQuery);
        List<EventData> data = event.getData();

        String jsonString = "{\"alarm_type\":0,\"alive_type\":0,\"appear_count\":0,\"camera_name\":\"通道1\",\"channel\":1,\"device_id\":\"ID\",\"event_type\":0,\"face_attr\":{\"cap_style\":\"hat_style_type_none\",\"gender_code\":\"male\",\"glass_style\":\"transparent_glasses\",\"mustache_style\":\"mustache_style_type_none\",\"respirator_color\":\"color_type_none\",\"st_age\":\"st_adult\",\"st_age_value\":\"33.000000\",\"st_expression\":\"st_angry\",\"st_helmet_style\":\"st_helmet_style_type_none\",\"st_respirator\":\"st_respirator_mouth\"},\"img\":\"abc\",\"img_id\":\"v214_1701621271_237_0\",\"img_path\":\"facebase/libid2/2023/12/04/v214_1701621271_237_0.png\",\"lib_id\":2,\"lib_name\":\"测试\",\"lib_type\":2,\"liveness_score\":0,\"liveness_thr\":0,\"obj_label\":1,\"ora_pos_bottom\":0,\"ora_pos_left\":0,\"ora_pos_right\":0,\"ora_pos_top\":0,\"person_addr\":\"\",\"person_age\":\"\",\"person_gender\":\"\",\"person_idcard\":\"\",\"person_name\":\"myj\",\"pos_bottom\":549,\"pos_left\":712,\"pos_right\":898,\"pos_top\":363,\"position\":\"\",\"quality\":86,\"ranking\":1,\"similarity\":36,\"snap_buf\":\"abcd\",\"snap_id\":\"v214_1730646287_8097_0\",\"snap_path\":\"record/face/channel1/2024/11/03/v214_1730646287_8097_0.jpg\",\"stranger_appear_channel\":0,\"threshold\":85,\"trigger\":\"2024-11-03 23:04:47\",\"wander_channels\":\"\",\"wander_deviceID\":\"\",\"wander_thresHold\":30,\"wander_trigger\":\"\"}"; // 替换为实际的 JSON 字符串

        ObjectMapper objectMapper = new ObjectMapper();
        List<AlarmData> alarmDatas = new ArrayList<>();
        try {
            for (int i = 0; i < data.size(); i++) {
                EventData eventData = data.get(i);
                AlarmData alarmData = objectMapper.readValue(jsonString, AlarmData.class);
//                byte[] bytes = imageService.downloadImage("IAkgIAAAAAACAAAAAAAAAAAAAAAAAAAAaPwhNtQt2wEFAIA4wkhRvLHry0KKN8oty7jmFgIAAAAUAHkAAAAAAAAAAAA=");
//                BASE64Encoder encoder = new BASE64Encoder();
//                // 去除标准中的回车符
//                String s = encoder.encode(bytes).replaceAll("[\\s*\t\n\r]", "");
                // 现在你可以使用 alarmData 对象了
                alarmData.setImg_path("/image/edge?file=" + eventData.getTarget_image_path());
                alarmData.setSnap_path("/image/edge?file=" + eventData.getScene_image_path());
                alarmData.setPerson_name("");
                alarmData.setSimilarity(99);
                alarmData.setLib_id(9);
                alarmData.setLib_type(9);
                alarmData.setTrigger(TimeUtils.parseStr(eventData.getTimestamp()));
                alarmDatas.add(alarmData);
            }

            HttpResponse httpResponse = HttpResponse.successData(alarmDatas);
            httpResponse.setCode(0);
            return httpResponse;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping("/alarm/list")
    public HttpResponse getAlarmList(){
        List<AlarmData> eventList = alarmService.getEventList();
        HttpResponse httpResponse = HttpResponse.successData(eventList);
        httpResponse.setCode(0);
        return httpResponse;
    }
}
